package com.zl.controller;

import com.zl.beans.NewsLabel;
import com.zl.beans.Page;
import com.zl.service.NewsLableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class NewsLabelController {

    @Autowired
    private NewsLableService service;
    /*分页查询*/
    @RequestMapping("/queryNewsLabel")
    /*按当前页面查询，默认页面值为1*/
    public String queryNewsLabel(@RequestParam(defaultValue = "1") Integer pagenum, Model model){
        Page<NewsLabel> page = service.findCurrentPage(pagenum);
        Page<NewsLabel> labels = service.findLibels();
        for (NewsLabel ns:page.getDatas()){
            System.out.println(ns);
        }
        model.addAttribute("labels",labels);
        model.addAttribute("page",page);
        return "/html/news/LabelManage.jsp";
    }
    /*添加*/
    @RequestMapping("/addLabel")
    public String addLabel(Integer flag,String labelName,String labelContent,Integer pid,Model model){
        if(flag==1){
            Page<NewsLabel> labels = service.findLibels();
            model.addAttribute("labels",labels);
            return "/html/news/addLabel.jsp";
        }
        else{
            System.out.println(labelName+labelContent+pid);
            service.addLabel(labelName,labelContent,pid);
            return "redirect:/queryNewsLabel";
        }
    }
    /*删除栏目*/
    @RequestMapping("/delectLabel")
    public String delectLabel(Integer id){
        service.rmLabel(id);
        return "redirect:/queryNewsLabel";
    }
    /*查找下拉框子栏目*/
    @RequestMapping("/queryChild")
    public String queryChild(Integer value,Model model){
        Page<NewsLabel> page = service.findChild(value);
        Page<NewsLabel> labels = service.findLibels();
        for (NewsLabel ns:page.getDatas()){
            System.out.println(ns);
        }
        model.addAttribute("labels",labels);
        model.addAttribute("page",page);
        return "/html/news/LabelManage.jsp";
    }
    /*修改栏目*/
    @RequestMapping("/updateLabels")
    public String updateLabels(
            Integer flag,Integer id,Model model,
            Integer pid,String labelName,String labelContent){
        if(flag==1){
            Page<NewsLabel> labels = service.findLibels();
            model.addAttribute("labels",labels);
            NewsLabel newsLabel = service.SelectLabelById(id);
            model.addAttribute("newsLabel",newsLabel);
            System.out.println(newsLabel+"88556");
            return "/html/news/newsLabelUpdate.jsp";
        }else{
            System.out.println("栏目Id:"+id+"   栏目名称："+labelName+"   栏目描述："+labelContent+"   上级Id："+pid);
            service.updateLabel(labelName,labelContent,id,pid);
            return "redirect:/queryNewsLabel";
        }
    }
}
